package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.database.DataBase;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.util.StrUtils;
import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.ImageExtend;
import cn.sccl.dirms.monitor.model.SysImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图标数据
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/image")
public class ImageController {



    @Autowired
    private DataBase dataBase;



    @RequestMapping (value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public String iconUpLoad(@RequestParam("file") MultipartFile attachment, HttpServletRequest request) throws IOException {

        String iconId = "";
        if(attachment!=null ){
            ImageExtend image = convertMultipartFileToImageExtend(attachment);
            iconId = this.addImage(image);
        }
        return iconId;
    }

    private ImageExtend convertMultipartFileToImageExtend(MultipartFile attachment) {
        ImageExtend image = new ImageExtend();
        image.setName(attachment.getOriginalFilename());
        image.setImageformat(attachment.getContentType());
        try{
            image.setImage(attachment.getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }

        return image;
    }


    // sql方式添加图标
    public String addImage(ImageExtend image) {
        String uuid = StrUtils.generateShortUuid();
        String sql = "insert into images(imageid, imagetype, name, image, imageformat) " +
                "values(?,?,?,?,?)";
        List params = new ArrayList();
        params.add(uuid);
        params.add("1");
        params.add(image.getName());
        params.add(image.getImage());
        params.add(image.getImageformat());
        int ret = this.dataBase.executeUpdate(sql, params.toArray());
        if (ret <= 0) {
            return null;
        }
        return uuid;
    }
    /**
     * 根据mapid获取图标数据
     * @param mapid 图标id
     */
    @RequestMapping(value = "/getByMapid/{mapid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> getImageByMapId(@PathVariable String mapid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
        params.put("sysmapids", mapid);
        params.put("select_image", true);
        Map<String, Object> result = zabbix.call("image.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> imageList = new HashMap<>();
        Map<String, ImageExtend> allSysIconsMap = this.getSysIcons();
        for (Map<String, Object> item: mapList) {
            imageList.put((String) item.get("imageid"), allSysIconsMap.get(item.get("imageid")));
        }
        return imageList;
    }

    /**
     * 获取所有系统图标
     */
    @RequestMapping(value = "/getSysIcons", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, ImageExtend> getSysIcons() {

//        Zabbix zabbix = Zabbix.getZabbix();
//        Map<String,Object> params = new HashMap<String,Object>();
//        params.put("output", "extend");
//        params.put("select_image", true);
//        Map<String, Object> result = zabbix.call("image.get", params);
//        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");

        List<ImageExtend> imageExtends = this.listAll();
        Map<String, ImageExtend> imageMap = new HashMap<>();
        for (ImageExtend item: imageExtends) {
            imageMap.put(item.getImageid(), item);
        }
        return imageMap;
    }

    /**
     * 创建图标
     */
//    @RequestMapping(value = "/create", method = {RequestMethod.POST,RequestMethod.GET})
//    public Map<String,Object> createImage(@RequestBody Image image) {
//        Zabbix zabbix = Zabbix.getZabbix();
//        Map<String,Object> params = new HashMap<String,Object>();
//        params.put("imagetype", image.getImagetype());
//        params.put("name", image.getName());
//        params.put("image", image.getImage());
//        Map<String, Object> result = zabbix.call("image.create", params);
//        return (Map<String, Object>) result.get("result");
//    }

    /**
     * 获取hostid和image的关系
     */
    @RequestMapping(value = "/getImageAndHostid", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, SysImage> getImageAndHostid() {
        String sql = "select ht.hostid, m.imageid from images m, t_dist_template_icons tns, hosts_templates ht " +
                " where m.imageid=tns.icons_id and tns.template_id=ht.templateid and " +
                " tns.delete_flag=0";
        List<SysImage> sysImages = this.dataBase.query4Model(sql, SysImage.class);

        Map<String, SysImage> sysImageMap = new HashMap<>();
        for (SysImage item: sysImages) {
            sysImageMap.put(item.getHostid(), item);
        }
        return sysImageMap;
    }

    /**
     * 分页获取获取所有图标数据
     */
    @RequestMapping(value = "/pagequery/{pageNo}/{pageSize}", method = {RequestMethod.POST,RequestMethod.GET})
    public PageResult<ImageExtend> pageQuerySysMap(@PathVariable long pageNo, @PathVariable  int pageSize) {
        String sql = "select s.imageid, s.imagetype, s.name, s.image, s.imageformat from images s where s.imageid>187";
        return this.dataBase.query4Model(sql, pageNo, pageSize, ImageExtend.class);
    }

    /**
     * sql获取所有图标数据
     */
    @RequestMapping(value = "/all", method = {RequestMethod.POST,RequestMethod.GET})
    public List<ImageExtend> listAll() {
        String sql = "select s.imageid, s.imagetype, s.name, s.image, s.imageformat from images s";
        return this.dataBase.query4Model(sql, ImageExtend.class);
    }

    /**
     * 删除图标
     * @param imageid 图标id
     */
    @RequestMapping(value = "/delete/{imageid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> deleteImage(@PathVariable String imageid) {
        Zabbix zabbix = Zabbix.getZabbix();
        List params = new ArrayList();
        params.add(imageid);
        Map<String, Object> result = zabbix.call("image.delete", params);
        return result;
    }
}
